<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *   Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

require_once("entity.class.php");
 
class team extends entity {

	var $id;
	var $name;
	var $status;
	var $lang;
	var $prid;
	var $subprj;
	
	public function __construct()
   {
   }

	// THIS CREATE A NEW INSTANCE OF THIS CLASS
	public function newInstance() {
		//$class = new ReflectionClass('user');	
		//return $class->newInstance();
		return new team;
	}

	// COMMON PUBLIC FUNCTIONS // NEEDED FOR LOADER
	public function get() {
		$sql = "SELECT * FROM ".TEAMS." WHERE id = ".util::str($this->id);
		return $sql;
	}

	public function listAll() {
		$sql = "SELECT * FROM ".TEAMS." ORDER BY name";
		return $sql;
	}
	
	public function listByStatus() {
		$sql = "SELECT * FROM ".TEAMS." WHERE status = ".util::str($this->status)." ORDER BY name";
		return $sql;
	}
	
	public function listByNotReady() {
		$FPR = FILES.PROJECT_PREFIX.$this->prid;
		$sql = "SELECT * FROM ".TEAMS." t WHERE status = ".util::str(STATUS_ACTIVE)." AND NOT EXISTS(SELECT f.id FROM ".$FPR." f WHERE f.teamid = t.id AND f.prjid = ".util::str($this->subprj).")";
		return $sql;
	}

	public function listByReady() {
		$FPR = FILES.PROJECT_PREFIX.$this->prid;
		$sql = "SELECT * FROM ".TEAMS." t WHERE status = ".util::str(STATUS_ACTIVE)." AND EXISTS(SELECT f.id FROM ".$FPR." f WHERE f.teamid = t.id AND f.prjid = ".util::str($this->subprj).")";
		return $sql;
	}
	
	public function insert() {
		$sql = "INSERT INTO ".TEAMS." (name,status,lang) ";
		$sql .= "VALUES (".util::str($this->name).",".util::str(STATUS_ACTIVE).",".util::str($this->lang).")";
		return $sql;
	}

	public function update() {
		$sql = "UPDATE ".TEAMS;
		$sql .= " set name = ".util::str($this->name);
		$sql .= ", status = ".util::str($this->status);
		$sql .= ", lang = ".util::str($this->lang);
		$sql .= " WHERE id = ".util::str($this->id);
		return $sql;
	}

	public function delete() {
		$sql = "DELETE FROM ".TEAMS." where id = ".util::str($this->id);
		return $sql;
	}
	
	public function fill($array) {
		$this->id=$array['id'];
		$this->name=$array['name'];
		$this->status=$array['status'];
		$this->lang=$array['lang'];
	}

}

?>
